package com.wr.recruit.service.apis;

import com.google.common.collect.Lists;
import com.wr.recruit.service.apis.dto.MessageDto;
import com.wr.recruit.service.dao.dbo.MessageDbo;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.service.MessageService;
import com.wr.recruit.service.service.UserService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;

@Controller
public class MessageApis {

    private MessageService messageService;
    private UserService userService;

    @Autowired
    public MessageApis(MessageService messageService,
                       UserService userService) {
        this.messageService = messageService;
        this.userService = userService;
    }

    @PostMapping(value = "/message/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsMessageCreate(@RequestBody MessageDto messageDto){
        UserDbo userInfo = userService.getUserInfo(messageDto.getUserName());
        if (Objects.isNull(userInfo))
            return new APIResponseSet(true,"创建招聘信息失败，请输入创建人");
        MessageDbo messageDbo = new MessageDbo();
        messageDbo.setCompanyCode(userInfo.getCompanyCode());
        messageDbo.setTitle(messageDto.getTitle());
        messageDbo.setContent(messageDto.getContent());
        messageDbo.setMail(userInfo.getMail());
        messageDbo.setSalary(messageDto.getSalary());
        messageDbo.setCreatedAt(new Timestamp(new Date().getTime()));
        messageDbo.setRevoked(false);
        MessageDbo message = messageService.createMessage(messageDbo);
        return new APIResponseSet(true,"创建招聘成功", Lists.newArrayList(message));
    }

    @PostMapping(value = "/message/update", produces = "application/json")
    @ResponseBody
    public APIResponse rsMessageUpdate(@RequestBody MessageDto messageDto){
        UserDbo userInfo = userService.getUserInfo(messageDto.getUserName());
        if (Objects.isNull(userInfo))
            return new APIResponseSet(true,"更新招聘信息失败，请输入更新用户");
        MessageDbo message = messageService.getMessage(messageDto.getMessageId());
        if (Objects.isNull(message))
            return new APIResponseSet(true,"更新招聘信息失败，未找到该招聘信息");
        if (!Objects.isNull(messageDto.getTitle()))
            message.setTitle(messageDto.getTitle());
        if (!Objects.isNull(messageDto.getContent()))
            message.setContent(messageDto.getContent());
        if (!Objects.isNull(messageDto.getSalaryMax()))
            message.setSalary(messageDto.getSalary());
        if (!Objects.isNull(message.getMail()))
            message.setMail(messageDto.getMail());
        MessageDbo messageDbo = messageService.updateMessage(message);
        return new APIResponseSet(true,"更新招聘成功", Lists.newArrayList(messageDbo));
    }

    @PostMapping(value = "/message/delete", produces = "application/json")
    @ResponseBody
    public APIResponse rsMessageDelete(@RequestBody MessageDto messageDto){
        messageService.deleteMessage(messageDto.getUserName(),messageDto.getMessageId());
        return new APIResponseSet(true,"删除招聘成功");
    }

    @GetMapping(value = "/message", produces = "application/json")
    @ResponseBody
    public APIResponse rsMessageGet(@RequestParam(value = "companyCode", required = false) String companyCode,
                                    @RequestParam(value = "salaryMin", required = false) Double salaryMin,
                                    @RequestParam(value = "salaryMax", required = false) Double salaryMax,
                                    @RequestParam(value = "pageSize") Integer pageSize,
                                    @RequestParam(value = "pageNum") Integer pageNum){
        Page<MessageDbo> messageList = messageService.getMessageList(salaryMin, salaryMax, companyCode, pageSize, pageNum);
        return new APIResponseSet(true,"查询招聘成功",Lists.newArrayList(messageList));
    }

}
